package com.jf.core;

import java.io.File;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.jfinal.core.Controller;
import com.jfinal.kit.PathKit;
import com.jfinal.upload.UploadFile;
import com.shop.bean.UserBean;
import com.shop.utils.StringUtils;

/**
 * 父类  控制器
 * @author boshouhai
 *
 */
public class BaseController extends Controller{

	
	public Logger logger = LogManager.getLogger(this.getClass());
	
	public final static String WEB_INF_URL ="/WEB-INF/view/";
	
	public final static String WIEW_WEB ="/viw/";
	
	public final static int PAGE_SIZE = 55;
	
	/**
	 * 当前用户
	 * @return
	 */
	public UserBean getCurrentUser(){
		return getSessionAttr("currentUser");
	}
	
	/**
	 * 上传文件
	 * @param fileParaName
	 * @param path {"headpic or  newspic"}
	 * @return
	 */
	public String uploadFile(String fileParaName,String path){
		UploadFile file = getFile(fileParaName,path);
		if(file != null){
			String fileName = file.getOriginalFileName();//原始名称
			String postfix = fileName.substring(fileName.lastIndexOf("."));// 后缀格式
			String name = "/upload/"+path+"/"+StringUtils.getTimeCode()+postfix;
			file.getFile().renameTo(new File(PathKit.getWebRootPath()+name));
			return name;
		}
		return null;
	}
	
	public String uploadFile(String fileParaName,String path,String id){
		UploadFile file = getFile(fileParaName,path+"/"+id);
		if(file != null){
			String fileName = file.getOriginalFileName();//原始名称
			String postfix = fileName.substring(fileName.lastIndexOf("."));// 后缀格式
			String name = "/upload/"+path+"/"+id+"/"+StringUtils.getTimeCode()+postfix;
			file.getFile().renameTo(new File(PathKit.getWebRootPath()+name));
			return name;
		}
		return null;
	}
}
